package Thread;

/**
 * Created with IntelliJ IDEA.
 * Description: 单线程和多线程时间比较
 */
public class ThreadDemo8 {
    private final static long count = 10_0000_0000;

    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            long beg = System.currentTimeMillis();
            int a = 0;
            for (int i = 0; i < count; i++) {
                a++;
            }
            int b = 0;
            for (int i = 0; i < count; i++) {
                b++;
            }
            long end = System.currentTimeMillis();
            System.out.println("单线程结束时间：" + (end - beg));
        });
        t.start();

        test();
    }

    private static void test() throws InterruptedException {
        long beg = System.currentTimeMillis();
        Thread t1 = new Thread(() -> {
            int a = 0;
            for (int i = 0; i < count; i++) {
                a++;
            }
        });
        Thread t2 = new Thread(() -> {
            int a = 0;
            for (int i = 0; i < count; i++) {
                a++;
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        long end = System.currentTimeMillis();
        System.out.println("多线程结束时间：" + (end - beg));
    }
}
